package com.webanovare.model.db;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
 *
 * @author daniel
 */
// Cant name this table user, the database complains about this
@Entity(name="PERSON")
public class User implements Serializable {

    @Id
    @Column(nullable=false)
    private String username;
    @Column(nullable=false)
    private String passwd;

    private String lastSessionId = "";
    
    private String email = "";
    
    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "SUBJECT_GROUP")
    @Enumerated(EnumType.STRING)
    private final List<UserGroup> groups = new ArrayList<>();
    
    @OneToMany
    private List<PlayedGame> gamesPlayed = new ArrayList<>();
    
    public User() {}
    public User(String username, String password) {
        this.username = username;
        this.passwd = password;
    }
    public User(String username, String password, String email) {
        this.passwd = password;
        this.username = username;
        this.email = email;
    }
    public String getUsername() {
        return username;
    }
    public boolean correctPassword(String password) {
        return this.passwd.equals(password);
    }
    public boolean setPassword(String newPassword, String oldPassword) {
        if (correctPassword(oldPassword)) {
            this.passwd = newPassword;
            return true;
        }
        return false;
    }
    public String getLastSessionId() {
        return lastSessionId;
    }
    public void setSessionId(String sessionId) {
        this.lastSessionId = sessionId;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getEmail() {
        return email;
    }
    public void addGroup(UserGroup group) {
        groups.add(group);
    }
    public void removeGroup(UserGroup group) {
        groups.remove(group);
    }
    public List<UserGroup> getGroups() {
        return groups;
    }
    
    public void addPlayedGame(PlayedGame pg) {
        gamesPlayed.add(pg);
        Logger.getAnonymousLogger().log(Level.INFO,"gamesPlayed!??!?!?????!!! " + gamesPlayed+ " pg!??!??!?!??!! " + pg);
    }
    
    public List<PlayedGame> getPlayedGames() {
        return this.gamesPlayed;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if ((this.username == null) ? (other.username != null) : !this.username.equals(other.username)) {
            return false;
        }
        if ((this.passwd == null) ? (other.passwd != null) : !this.passwd.equals(other.passwd)) {
            return false;
        }
        return true;
    }
}
